package influx;

import main.ConMysql;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * 抓取的指标有：
 * Questions-客户端向服务器发送的语句数
 * Slow_queries-慢查询数
 * Innodb_row_lock_waits- InnoDB 存储引擎停下来获得某行的锁定的次数
 * Threads_connected-当前开放的连接
 * Threads_running-当前运行的连接
 * Connection_errors_internal-由服务器错误导致的失败连接数
 * Aborted_connects-尝试与服务器进行连接结果失败的次数
 * Connection_errors_max_connections-由 max_connections 限制导致的失败连接数
 * Innodb_buffer_pool_pages_total-缓冲池中的总页数
 * Innodb_buffer_pool_read_requests-向缓冲池发送的请求数
 * Innodb_buffer_pool_reads-缓冲池无法满足的请求数
 * Open_tables-打开表的数量
 * Queries--服务器执行的语句数
 *
 * @author hxl
 */
public class DataInsertInflux {
    /**
     * 查询所有状态指标
     */
    private static final String ALL_GLOBAL_STATUS = "SELECT * FROM performance_schema.global_status;";
    /**
     * 查询全局变量
     */
    private static final String GLOBAL_VARIABLES = "SELECT * FROM performance_schema.global_variables;";
    /**
     * 查询所需指标
     */
    private static final String GLOBAL_STATUS = "SELECT VARIABLE_NAME,VARIABLE_VALUE FROM performance_schema.global_status "
            + "where VARIABLE_NAME in ('Questions','Slow_queries','Innodb_row_lock_waits',"
            + "'Threads_connected','Threads_running','Connection_errors_internal',"
            + "'Aborted_connects','Connection_errors_max_connections','Innodb_buffer_pool_pages_total',"
            + "'Innodb_buffer_pool_read_requests','Innodb_buffer_pool_reads','Open_tables',"
            + "'Queries');";
    private static final Logger LOGGER = LoggerFactory.getLogger(ConMysql.class);

    public void insertInflux() {

        //和influxdb进行连接
        InfluxDBCon influxdbcon = new InfluxDBCon();
        InfluxdbService influxSer = influxdbcon.influxCon();

        //连接mysql
        ConMysql con = new ConMysql();
        try (Connection connection = con.getCon()) {

            //构造fields
            Map<String, String> status = con.getMysqlData(connection, GLOBAL_STATUS);
            Map<String, Object> fields = new HashMap<>(status);

            //先构造一个tags
            Map<String, String> tags = new HashMap<>();
            //加载hostname文件获取hostname
            String hostname = null;
            try (BufferedReader br = new BufferedReader(new FileReader("/etc/hostname"))) {
                hostname = br.readLine();
            } catch (FileNotFoundException e) {
                LOGGER.error("/etc/hostname file not exist!");
                e.printStackTrace();
            } catch (IOException e) {
                LOGGER.error("/etc/hostname file read error!");
                e.printStackTrace();
            }
            tags.put("host", hostname);

            // 向数据库mysql表中插入数据
            influxSer.insert("mysql", tags, fields);
            LOGGER.info("获取Mysql指标成功！");
        } catch (SQLException e) {
            LOGGER.error("sql exec error!");
            e.printStackTrace();
        }

    }

}
